<?PHP
class tagSelect extends tagControl
{
	var $name = "";
	private $SelectedIndex = 0;
	private $items = array();

	var $EventChange = "tagSelect.change";
	function tagSelect( $attrs = array() )
	{
		parent::tagControl($attrs);
		if ( isset($attrs['name']) )
		{
			$this->name = $attrs['name'];
		}
	}
	function getSelectedIndex()
	{
		return $this->SelectedIndex;
	}
	private function ClearSelectedItems()
	{
		foreach ( $this->items as $num => $item )
		{
			$this->items[$num]->Selected = false;
		}
	}
	function setSelectedIndex($value)
	{
		$this->ClearSelectedItems();
		$this->SelectedIndex = $value;
		$option = $this->items[$value];
		$option->Selected = true;
	}
	function addControl($control)
	{
		if ( get_class($control) == 'tagOption' )
		{
			array_push($this->items, $control);
			if ( $control->Selected )
			{
				$this->SelectedIndex = count($this->items) - 1;
			}
		}
	}
	function Render($writer)
	{
		$writer->Write("<select");
		$id = $this->getId();
		if ( !empty($id) )
		{
			$writer->Write(" id=\"$id\"");
		}
		if ( !empty($this->name) )
		{
			$writer->Write(" name=\"$this->name\"");
		}
		foreach ( $this->attributes as $key=>$value )
		{
			if ($key == 'id' || $key == 'name' )
				continue;
				
			$writer->Write(" $key=\"$value\"");
		}
		$writer->Write(">\n");
		foreach ( $this->items as $num => $item )
		{
			$item->Render($writer);
		}
		$writer->Write("</select>");
	}
	// Event Change {
	function addChange($handler)
	{
		$this->addEventHandler($this->EventChange, $handler);
	}
	function removeClick($handler)
	{
		$this->removeEventHandler($this->EventChange, $handler);
	}
	// } End: Event Change
	function RiseEvents()
	{
		if ( $this->hasEvent($this->EventChange) )
		{
			$this->getEvent($this->EventChange)->call($this, array());
		}
	}
}
?>
